package com.gxa.user.recycling.service.impl;

import com.github.pagehelper.PageInfo;
import com.gxa.core.recycling.common.ExceptionCode;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.*;
import com.gxa.core.recycling.service.BaseService;
import com.gxa.core.recycling.service.impl.BaseServiceImpl;
import com.gxa.core.recycling.util.DateUtil;
import com.gxa.core.recycling.util.GetTimeUtil;
import com.gxa.user.recycling.mapper.UsersMapper;
import com.gxa.user.recycling.service.GoodsService;
import com.gxa.user.recycling.service.MemberLevelService;
import com.gxa.user.recycling.service.ScoreService;
import com.gxa.user.recycling.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("usersService")
@Transactional(rollbackFor = {Throwable.class})
public class UsersServiceImpl extends BaseServiceImpl<User> implements UsersService {





    @Override
    public void update(Long id,String location) {
        User user = super.selectById(id);
        user.setLocation(location);
        super.updateById(user);
    }

    @Override
    public void uodateName(Long id, String name) {
        User user = super.selectById(id);
        user.setName(name);
        super.updateById(user);
    }

    @Override
    public void updatePhone(Long id, String phone) {
        User user = super.selectById(id);
        user.setPhone(phone);
        super.updateById(user);
    }

    @Override
    public void updateState(Boolean state, Long id) {
        usersMapper.updateState(state, id);
    }

    @Override
    public long judgeState(Long id) {
        Date date = new Date(System.currentTimeMillis());
        long time = date.getTime();
        Date date1 = GetTimeUtil.initDateByDay();
        long time1 = date1.getTime();
        long timeNext=time1+86400000;
        long timetchuo=timeNext-time;
        User user = super.selectById(id);
        Boolean state = user.getState();
        if(state){
            return timetchuo;
        }else{
            return -1;
        }
    }

    @Override
    public void updateState1(Long id) {
        usersMapper.updateState1(id);
    }

    @Autowired
    UsersService usersService;
    @Override
    public Integer keepState(Long id) throws BusinessException {
        Long id1 = scoreService.getMaxScoreId("签到",id);
        if(id1==null){
           //throw new BusinessException(ExceptionCode.NO_QIANDAO);
            return 0;
        }
        Score score = scoreService.selectById(id1);
        Date changetime = score.getChangetime();
        long time = changetime.getTime();
        long time2=GetTimeUtil.initDateByDay().getTime()+86400000;
        long timeNowDay=GetTimeUtil.initDateByDay().getTime();
        Date date = new Date(System.currentTimeMillis());
        long time1 = date.getTime();
        if(time1>time&&time2>time1&&time>timeNowDay){
            return 1;
        }
        User user = usersService.selectById(id);
        user.setState(false);
        usersService.updateById(user);
        return 0;
    }

    @Autowired
    UsersMapper usersMapper;
    @Override
    public List<User> selectLocation(String location) throws BusinessException {
        List<User> list = usersMapper.selectLocation(location);
        if(list==null){
            throw new BusinessException(ExceptionCode.LOCATION_WRONG);
        }
        return list;
    }
    @Autowired
    ScoreService scoreService;

    @Override
    public Map send(Long uid) {
        Map map = new HashMap();
        User user = super.selectById(uid);
        String img = user.getImg();
        Integer nowPoints = user.getNowPoints();
        Date lastLogin = user.getLastLogin();
        String weekOfDate = DateUtil.getWeekOfDate(lastLogin);
        Score score = new Score();
        score.setUid(uid);
        int count=0;
        List<Score> select = scoreService.select(score);
        for (Score score1 : select) {
            String serson = score1.getSerson();
            if(serson.equals("签到")){
                count++;
            }
        }
        map.put("count",count);
        map.put("img",img);
        map.put("points",nowPoints);
        map.put("week",weekOfDate);
        return map;


    }
    @Autowired
    MemberLevelService memberLevelService;
    @Override
    public Map haveScore(Long id) {
        User user = super.selectById(id);
        Map map = new HashMap();
        Integer allPoints = user.getAllPoints();
        if(allPoints<=2000){
            int points=2000-allPoints;
            map.put("points",points);
            map.put("name","升级为环保新人");
        }
        if(allPoints>2000&&allPoints<=5000){
            int points=5000-allPoints;
            map.put("points",points);
            map.put("name","升级为环保卫士");
        }
        if(allPoints>5000&&allPoints<=10000){
            int points=10000-allPoints;
            map.put("points",points);
            map.put("name","升级为环保达人");
        }
        if(allPoints>10000&&allPoints<=20000){
            int points=20000-allPoints;
            map.put("points",points);
            map.put("name","升级为环保使者");
        }
        if(allPoints>20000){
            map.put("points",null);
            map.put("name","恭喜您已经成为环保掌门");
        }
        return map;


    }

}
